package com.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/25 12:36
 * @since JDK 1.8
 */
public class Utils {


    private static SimpleDateFormat dft=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    /**
     * 比较两个字符串是否相等
     * @param str1
     * @param str2
     * @return
     */
    public static boolean isEquery(String str1,String str2){
        if(str1==null||str2==null){
            return false;
        }
        if(str2.equals(str1)){
            return true;
        }
        return false;
    }


    /**
     * 对象的Map转化
     * @param t 对象
     * @return 反射
     */
    public static <T> Map<String,Object> objectMap(T t){
        Class<T> tClass=(Class<T>) t.getClass();
        Field[] fields=tClass.getDeclaredFields();
        Map<String,Object> objectMap=null;
        try {
            objectMap=new HashMap<>(fields.length);
            for (int i = 0; i < fields.length; i++) {
                objectMap.put(fields[i].getName(),fields[i].get(t));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return objectMap;

    }

    /**
     *
     * @param t 对象
     * @param feildName 该对象的属性名称
     * @return  该对象的属性数据
     */
    public static <T> Object getValueByFeildName(T t, String feildName){
        Class<T> cls=(Class<T>) t.getClass();
        Field[] fields=cls.getDeclaredFields();
        try {
            for (int i = 0; i < fields.length; i++) {
                Field field=fields[i];
                if(!field.getName().equals(feildName)){
                    continue;
                }
                return field.get(t);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 方法调用
     * @param cls 反射
     * @param method 方法名
     * @param args 参数数组
     * @return  调用执行的结果
     */
    public static <T> Object invokeMethod(Class<T> cls,String method,Object ... args){
        T object=getObjectByClazz(cls);
        Method method1=getMethod(cls,method);
        try {
            return method1.invoke(object,args);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过方法名获取反射的方法
     * @param cls 反射
     * @param method 方法名
     * @return 反射的方法
     */
    public static Method getMethod(Class<?> cls,String method){
        Method[] methods=cls.getDeclaredMethods();
        for (int i = 0; i < methods.length ; i++) {
            Method method1=methods[i];
            if(!method1.getName().equals(method)){
                continue;
            }
            return method1;
        }
        return null;
    }

    /**
     *
     * @param cls 反射
     * @return 属性列表
     */
    public static <T> List<String> feildList(Class<T> cls){
        Field [] fields=cls.getDeclaredFields();
        List<String> stringList=null;
        if(fields.length>0){
            stringList=new ArrayList<>(fields.length);
            for (int i = 0; i < fields.length; i++) {
                stringList.add(fields[i].getName());
            }
        }
        return stringList;
    }



    /**
     * 通过反射获取类的对象
     * @param cls 反射
     * @param <T> 参数需要指向的对象
     * @return 对象
     */
    public static <T> T getObjectByClazz(Class<T> cls){
        T object=null;
        try {
            object=cls.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        return object;
    }

    /**
     * 获取类路径的反射
     * @param str 类路径
     * @return 该类的反射信息
     */
    public static <T> Class<T> getCLazzByString(String str){
        Class<T> clazz=null;
        try {
            Class<T> cls=(Class<T>) Class.forName(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return clazz;
    }


    public static Field getFieldByName(String name,Field[] fields){
        for (int i = 0; i < fields.length; i++) {
            Field field=fields[i];
            String fieldName = field.getName();
            if(fieldName.equals(name)){
                return field;
            }
        }
        return null;
    }


    /**
     * 获取当前时间
     * @return
     */
    public static String getNowDateString(){
        return dft.format(new Date());
    }


    /**
     * 命令行运行
     * @return
     */
    public static String runTimeProcess(String shell){
        StringBuilder stringBuilder=new StringBuilder();
        try {
            Process process = Runtime.getRuntime().exec(shell);
            InputStreamReader ir = new InputStreamReader(process
                    .getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            process.waitFor();
            while ((line = input.readLine()) != null){
                System.out.println(line);
                stringBuilder.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    public static String runTimeProcess(String[] cmds){
        StringBuilder stringBuilder=new StringBuilder();
        //String[] cmd = new String[]{"/bin/sh","-c", " ps -ef"};
        try {
            Process ps = Runtime.getRuntime().exec(cmds);
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


}
